package com.example.a2209_shop.utils

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer

/**
 * 解决数据倒灌
 */
class SingleLiveData<T> : MutableLiveData<T>() {
    private var mPending = false
    override fun observe(owner: LifecycleOwner, observer: Observer<in T>) {
        super.observe(owner, Observer<T>{t->
            if(!mPending){
                observer.onChanged(t)
            }
        })
    }
    override fun setValue(value: T) {
        mPending = true
        super.setValue(value)
    }
    override fun postValue(value: T) {
        mPending = true
        super.postValue(value)
    }
}